<template>
	<view class="list-view">
		<view class="top-bar"><uni-navbar :show="show" @click-show="show = !show" :fixed="true" :back="true" color="#fff" title="问题列表"></uni-navbar></view>

		<view class="filter-box" v-if="show">
			<view class="form-box">
				<u-form ref="uForm">
					<u-form-item class="form-item" label="问题标题">
						<!-- block -->
						<u-input v-model="clue.title" type="text" placeholder="请输入问题标题" />
					</u-form-item>
					<u-form-item class="form-item" label="镇街/市直代表小组" labelWidth="300">
						<u-input v-model="dept" type="select" :select-open="deptShow" placeholder="请选择类型" @click="deptShow = true" />
						<u-select :list="deptList" v-model="deptShow" @confirm="deptConfirm"></u-select>
					</u-form-item>
					<u-form-item class="form-item" label="提交人">
						<!-- block -->
						<u-input v-model="clue.submitUser" type="text" placeholder="请输入提交人" />
					</u-form-item>
					<u-form-item class="form-item" label="选择代表">
						<!-- block -->
						<u-input v-model="clue.userName" type="text" placeholder="请输入代表" />
					</u-form-item>
					<u-form-item class="form-item" label="开始时间">
						<!-- block -->
						<date-time-picker placeholder="请选择开始时间" :defaultValue="startTime" :old="true" fields="day" @change="timeChange($event, 'startTime')"></date-time-picker>
					</u-form-item>
					<u-form-item class="form-item" label="结束时间">
						<!-- block -->
						<date-time-picker placeholder="请选择结束时间" :defaultValue="endTime" :old="true" fields="day" @change="timeChange($event, 'endTime')"></date-time-picker>
					</u-form-item>
				</u-form>
			</view>
			<view class="fiexd-btm">
				<view class="flex-box">
					<u-button class="form-btn" type="default" @click="resert">清除</u-button>
					<u-button class="form-btn" type="error" @click="filter">确定</u-button>
				</view>
			</view>
		</view>

		<view class="search-view"><u-search class="search-input" shape="square" bg-color="#fff" v-model="clue.title" :show-filter="true" @search="search" @custom="navTo('/pages/clue/search')"></u-search></view>
		<u-tabs class="tab-box" :list="tabsList" :current="tabsCurrent" @change="tabsChange" :is-scroll="false" :key="tabsList.length"></u-tabs>

		<view class="list-box" :style="{ background: loading == 'nodata' ? 'none' : '#fff' }">
			<!-- <view class="list-info_01" v-for="(item, index) of clueList" :key="item.id" @click="navTo('/pages/clue/detail?id=' + item.id)">
				<view class="info-title">{{ item.title }}</view>
				<view class="info-btm">
					<view class="btm-left clamp">
						<text class="left-name">{{ item.submitUser }}</text>
						<text class="left-time">{{ item.submitTime || $parseTime(item.createTime, '{y}-{m}-{d}') }}</text>
					</view>
					<view class="btm-right">
						<view class="right-type" :style="'color:' + colorMap[item.nodeStatus]">{{ item.nodeStatusName }}</view>
					</view>
				</view>
			</view> -->

			<view class="list-info_item" v-for="(item, index) of clueList" :key="item.id">
				<view class="list-info_02" @click="navTo('/pages/clue/detail?id=' + item.id)">
					<view class="left">
						<view class="info-title">{{ item.title }}</view>
						<view class="info-btm">
							<view class="left-name">{{ item.submitUser }}</view>
							<view class="line"></view>
							<text class="left-time">{{ item.submitTime || $parseTime(item.createTime, '{y}-{m}-{d}') }}</text>
						</view>
					</view>
					<view class="right" :style="'color:' + colorMap[item.nodeStatus]">{{ item.nodeStatusName }}</view>
				</view>
				<view class="info-btn">
					<u-button class="sub-btn" v-if="item.nodeStatus == 'wait' && $sysBtns('msjdb_wait_chuli')" type="error" plain size="medium" shape="circle" @click="handle(item)">处理</u-button>
					<u-button class="sub-btn" v-if="['1'].indexOf(item.nodeStatus) > -1 && $sysBtns('msjdb_1_banjie')" type="error" plain size="medium" shape="circle" @click="handle(item)">处理</u-button>
					<u-button class="sub-btn" v-if="['2'].indexOf(item.nodeStatus) > -1 && $sysBtns('msjdb_2_chuli')" type="error" plain size="medium" shape="circle" @click="handle(item)">处理</u-button>
					<u-button class="sub-btn" v-if="['3'].indexOf(item.nodeStatus) > -1 && $sysBtns('msjdb_3_banli')" type="error" plain size="medium" shape="circle" @click="handle(item)">办理</u-button>
					<u-button class="sub-btn" v-if="['21'].indexOf(item.nodeStatus) > -1 && $sysBtns('msjdb_21_chuli')" type="error" plain size="medium" shape="circle" @click="handle(item)">处理</u-button>
					<u-button class="sub-btn" v-if="item.nodeStatus == 'confirm' && $sysBtns('msjdb_confirm_banjie')" type="error" plain size="medium" shape="circle" @click="handle(item)">确认</u-button>
				</view>
			</view>

			<u-loadmore :status="loading" />
		</view>

		<view class="btn-add" v-if="$sysMenus('msjdb_edit')" @click="navTo('/pages/clue/add_db')"><u-icon name="plus" color="#fff" size="38"></u-icon></view>

		<u-back-top :scroll-top="scrollTop" top="200"></u-back-top>
	</view>
</template>

<script>
import dateTimePicker from '@/components/uni-datetime-picker';
import { mapState } from 'vuex';
export default {
	components: {
		dateTimePicker
	},
	data() {
		return {
			scrollTop: 0,
			show: false,
			tabsCurrent: 0,
			tabsList: [],
			colorMap: {
				0: '#0099ff',
				1: '#ff9900',
				2: '#19be6b',
				3: '#00ff00',
				4: '#00ffff',
				5: '#F5A623'
			},
			title: '',
			type: 'clue',
			clueList: [],

			dept: '',
			deptShow: false,
			deptList: [],

			startTime: '',
			endTime: '',

			clue: {
				title: '',
				submitUser: '',
				dateRange: '',
				userName: '',
				createName: '',
				deptId: '',
				nodeStatus: ''
			},
			loading: 'loadmore'
		};
	},
	onLoad(option) {
		this.loadLogin();
		if (option && option.type) {
			this.type = option.type;
		}
		this.loadData();
		this.loadDept();
		this.loadDict('represent_type', 'tabsList');
	},
	onPullDownRefresh() {
		this.loadData('refresh');
	},
	onReachBottom() {
		if (this.loading == 'loadmore') this.loadData('add');
	},
	onPageScroll(res) {
		this.scrollTop = res.scrollTop;
	},
	computed: {
		dateRangeSea: {
			get: function() {
				let time = this.startTime + ' - ' + this.endTime;
				if (this.startTime == '' || this.endTime == '') {
					return '';
				} else {
					return time;
				}
			},
			set: function(e) {
				this.startTime = '';
				this.endTime = '';
			}
		},
		...mapState(['userInfo'])
	},
	methods: {
		loadLogin() {
			if (this.userInfo.sysUser) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.userId = this.userInfo.sysUser.id;

				this.pageNum = 1;
				this.pageSize = 10;
			} else {
				// this.$logout();
			}
		},
		loadData(type = 'add') {
			if (type == 'add' && this.loading == 'loading') {
				return false;
			}
			if (type == 'add' && this.loading == 'nomore') {
				return false;
			}

			this.loading = 'loading';
			if (type != 'add') {
				this.pageNum = 1;
			}
			let data = {
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				currentUserId: this.userId,
				clientCode: this.clientCode,
				...this.clue
			};
			let url = '/lvzhitong/represent/list';

			this.$request(url, data, {
				load: true
			}).then((res) => {
				let count = 0;
				if (type != 'add') {
					uni.stopPullDownRefresh();
					this.clueList = [];
				}
				this.loading = this.pageNum < Math.ceil(res.data.total / this.pageSize) ? 'loadmore' : 'nomore';
				this.pageNum = this.pageNum + 1;
				this.clueList = this.clueList.concat(res.data.records);
				this.loaded = true;
				if (this.clueList.length <= 0) {
					this.loading = 'nodata';
				}
			});
		},
		loadDictType(type, list) {
			this.$request('/system/sys-dict/dictList', {
				clientCode: this.clientCode,
				sysDict: {
					dictType: type,
					remark: 'type'
				}
			}).then((res) => {
				if (!res.data) {
					return false;
				}
				res.data.forEach((item) => {
					this[list].push({
						name: item.dictValue,
						value: item.dictValue,
						label: item.dictName
					});
				});
			});
		},
		loadDict(type, list) {
			this.$request('/system/sys-dict/dictList', {
				clientCode: this.clientCode,
				sysDict: {
					dictType: type
				}
			}).then((res) => {
				if (!res.data) {
					return false;
				}
				this[list] = [];
				if (list == 'tabsList') {
					this.tabsList.push({
						name: '全部',
						value: ''
					});
				}

				res.data.forEach((item) => {
					this[list].push({
						value: item.dictValue,
						label: item.dictName,
						name: item.dictName
					});
				});
			});
		},
		loadDept() {
			this.$request('/system/sys-dept/depts', {
				clientCode: this.clientCode,
				jiedao: '1'
			}).then((res) => {
				if (!res.data) {
					return false;
				}
				this.deptList = [];
				res.data.forEach((item) => {
					this.deptList.push({
						id: item.id,
						value: item.id,
						label: item.deptName,
						name: item.deptName
					});
				});
			});
		},
		timeChange({ date, dt }, idx) {
			this[idx] = date.year + '-' + date.month2 + '-' + date.day2;
		},
		deptConfirm(e) {
			this.dept = e[0].label;
			this.clue.deptId = e[0].value;
		},
		tabsChange(index) {
			this.tabsCurrent = index;
			this.typeFunc(this.tabsList[index].value);
		},
		filter() {
			if (this.startTime == '' && this.endTime != '') {
				this.$msg('请选择开始时间');
				return false;
			}
			if (this.startTime != '' && this.endTime == '') {
				this.$msg('请选择结束时间');
				return false;
			}
			let arr = this.startTime.split('-');
			let startTime = new Date(arr[0], arr[1], arr[2]).getTime();

			let arrs = this.endTime.split('-');
			let endTime = new Date(arrs[0], arrs[1], arrs[2]).getTime();

			if (startTime >= endTime) {
				this.$msg('开始时间应小于结束时间');
				return false;
			}

			this.clue.dateRange = this.dateRangeSea;

			this.show = false;
			this.loadData('refresh');
		},
		resert() {
			this.show = false;
			this.clue.title = '';
			this.dept = '';
			this.clue.deptId = '';
			this.clue.submitUser = '';
			this.clue.dateRange = '';
			this.clue.userName = '';
			this.loadData('refresh');
		},
		search(e) {
			this.clue.title = e;
			this.loadData('refresh');
		},
		typeFunc(e) {
			this.clue.nodeStatus = e;
			this.loadData('refresh');
		},
		handle(item) {
			this.navTo('/pages/clue/detail?id=' + item.id);
		},
		navTo(url) {
			uni.navigateTo({
				url: url
			});
		}
	}
};
</script>

<style lang="scss">
.list-box {
}

.filter-box {
	position: fixed;
	left: 0;
	top: 0;
	bottom: 0;
	right: 0;
	z-index: 998;
	background-color: #fff;
}

.form-box {
	.form-item {
		@include mp-flex;
		border-bottom: 20rpx solid #f5f7fa;
	}
}
</style>
